package com.jcli.jb51_net.article._界面样式;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SwingMainUI extends JFrame {
    private JSplitPane mainPanel;
    private JPanel leftPanel;
    private JSplitPane rightPanel;
    private JPanel logPanel;
    private JButton btn1, btn2, btn3, btn4, btn5, btn6;
    private JLabel logLabel;
    private JTable table;

    public SwingMainUI() {
        setTitle("Swing Main UI");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 创建菜单栏
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu helpMenu = new JMenu("Help");
        JMenuItem exitItem = new JMenuItem("Exit");
        JMenuItem aboutItem = new JMenuItem("About");

        exitItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        aboutItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(SwingMainUI.this, "Swing Main UI Example", "About", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        fileMenu.add(exitItem);
        helpMenu.add(aboutItem);

        menuBar.add(fileMenu);
        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        // 创建主面板
        mainPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

        // 创建左边面板
        leftPanel = new JPanel(new GridLayout(6, 1));
        btn1 = new JButton("Button 1");
        btn2 = new JButton("Button 2");
        btn3 = new JButton("Button 3");
        btn4 = new JButton("Button 4");
        btn5 = new JButton("Button 5");
        btn6 = new JButton("Button 6");

        btn1.addActionListener(new ButtonClickListener());
        btn2.addActionListener(new ButtonClickListener());
        btn3.addActionListener(new ButtonClickListener());
        btn4.addActionListener(new ButtonClickListener());
        btn5.addActionListener(new ButtonClickListener());
        btn6.addActionListener(new ButtonClickListener());

        leftPanel.add(btn1);
        leftPanel.add(btn2);
        leftPanel.add(btn3);
        leftPanel.add(btn4);
        leftPanel.add(btn5);
        leftPanel.add(btn6);

        // 创建右边面板
        rightPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        rightPanel.setOneTouchExpandable(true);
        rightPanel.setDividerSize(10);

        // 默认显示表格内容
        showTableAndLogPanel();

        // 设置左右两个面板添加到主面板
        mainPanel.setLeftComponent(leftPanel);
        mainPanel.setRightComponent(rightPanel);

        // 设置左右两个面板的相对宽度比例
        // mainPanel.setDividerLocation(0.3);
        mainPanel.setResizeWeight(0.3);//固定宽度

        // 将主面板添加到窗口
        add(mainPanel);

        setVisible(true);
    }

    private class ButtonClickListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JButton source = (JButton) e.getSource();

            if (source == btn1) {
                showTableAndLogPanel();
            } else {
                showLogPanel(source.getText());
            }
        }
    }

    private void showTableAndLogPanel() {
        DefaultTableModel model = new DefaultTableModel();
        model.addColumn("Column 1");
        model.addColumn("Column 2");
        model.addRow(new Object[]{"Data 1-1", "Data 1-2"});
        model.addRow(new Object[]{"Data 2-1", "Data 2-2"});
        for (int i = 0; i < 248; i++) {
            model.addColumn("column "+i);
            model.addRow(new Object[]{"column " + i, "col" + (i + 1)});
        }

        table = new JTable(model);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // 设置为不自动调整列宽
        JScrollPane tableScrollPane = new JScrollPane(table);

        logPanel = new JPanel();
        logLabel = new JLabel("Log Area");
        logPanel.add(logLabel);

        // 设置日志面板的背景色
        logPanel.setBackground(Color.CYAN);

        rightPanel.setTopComponent(tableScrollPane);
        rightPanel.setBottomComponent(logPanel);
    }

    private void showLogPanel(String buttonText
    ) {
        rightPanel.setTopComponent(null);

        // 创建新的日志面板
        logPanel = new JPanel();
        logLabel = new JLabel("Log Area for " + buttonText);
        logPanel.add(logLabel);

        // 设置日志面板的背景色
        logPanel.setBackground(Color.YELLOW);

        rightPanel.setBottomComponent(logPanel);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(SwingMainUI::new);
    }
}
